/*
 * Copyright 2017-8-30 13:56:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.ao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.page.PageUtils;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.ao.JobAO;
import com.lap.scp.admin.core.JobManager;
import com.lap.scp.admin.domain.JobDO;
import com.lap.scp.admin.query.JobQueryDO;

/**
 * <pre>
 * 职位应用对象
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:26
 * @since 1.0
 */
@Service("jobAO")
public class DefaultJobAO extends AbstractAO implements JobAO {

	@Resource
	private JobManager jobManager;

	@Override
	public Resp<Integer> deleteById(int jobId) {
		Resp<Integer> resp = new Resp<>();
		try {
			int flag = jobManager.deleteById(jobId);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(jobId, e);
		}
		return resp;
	}

	@Override
	public Resp<JobDO> insertJob(JobDO jobDO, BindingResult error) {
		Resp<JobDO> resp = new Resp<>();
		try {
			super.validate(resp, error, jobDO);
			if (resp.isSuccess()) {
				jobManager.insertJob(jobDO);
				resp.setData(jobDO);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(jobDO, e);
		}
		return resp;
	}

	@Override
	public Resp<JobDO> queryById(int jobId) {
		Resp<JobDO> resp = new Resp<>();
		try {
			JobDO jobDO = jobManager.queryById(jobId);
			resp.setData(jobDO);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(jobId, e);
		}
		return resp;
	}

	@Override
	public PageResult<JobDO> queryPage(Page page, JobQueryDO jobQueryDO) {
		PageResult<JobDO> pr = new PageResult<>();
		try {
			List<JobDO> list = jobManager.queryPage(page, jobQueryDO);

			PageUtils.copyPage(pr, page);
			pr.setData(list);
		} catch (Exception e) {
			pr.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(new Object[] { page, jobQueryDO }, e);
		}
		return pr;
	}

	@Override
	public Resp<Integer> updateJob(JobDO jobDO, BindingResult error) {
		Resp<Integer> resp = new Resp<>();
		try {
			super.validate(resp, error, jobDO);
			if (resp.isSuccess()) {
				int flag = jobManager.updateJob(jobDO);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(jobDO, e);
		}
		return resp;
	}

}
